//
//  CXHBannerViewController.m
//  CXHAdSDK_Example
//
//  Created by 陈坤 on 2022/3/24.
//  Copyright © 2022 陈桑葚吃桑葚. All rights reserved.
//

#import "CXHBannerViewController.h"
#import <CXHAdSDK-Core/CXHAdSDK.h>
#import <CXHAdSDK-Core/CXHAdSDKBannerAdView.h>
#import <CXHAdSDK-Kit/CXHUtilsKit.h>
#import <CXHAdSDK-Core/CXHBWTAdContainerView.h>

@interface CXHBannerViewController ()<CXHSDKBannerAdViewDelegate, CXHBWTAdContainerViewDelegate, UIScrollViewDelegate>{
    CXHAdSDKBannerAdView *_bannerView;
}
@property (nonatomic, strong) CXHBWTAdContainerView *adContainerView;

@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, strong) UICollectionView *collectionView;

@end

@implementation CXHBannerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIBarButtonItem *itemLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(onCancelClicked:)];
    self.navigationItem.leftBarButtonItem = itemLeft;
    
    UIButton *btn2 = [UIButton new];
    btn2.backgroundColor = [UIColor orangeColor];
    btn2.layer.cornerRadius = 20;
    
    btn2.frame = CGRectMake((kCXHScreenWidth -150)/2, kCXHTopBarHeight + 50, 150, 40);
    [btn2 setTitle:@"load banner" forState:UIControlStateNormal];
    [btn2 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.view addSubview:btn2];
    [btn2 addTarget:self action:@selector(test2) forControlEvents:UIControlEventTouchUpInside];
    
    
}

- (void)onCancelClicked:(id)sender {
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - Banner
- (void)test2 {
    if (_bannerView) {
        [_bannerView removeFromSuperview];
        _bannerView = nil;
    }
    
    CGFloat height = [UIScreen mainScreen].bounds.size.width * 110/335;
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    
    // 1 初始化banner视图
    
    _bannerView = [[CXHAdSDKBannerAdView alloc] initWithFrame:CGRectMake(0, 0, width, height)];
    _bannerView.delegate = self;
    _bannerView.controller = self;
    _bannerView.refershTime = 35;
    _bannerView.nonstandard = YES;
//    _bannerView.posId = @"MSX_KP_00001";
    _bannerView.posId = @"79218763";
    _bannerView.backgroundColor = [UIColor whiteColor];
    
    // 2 添加到父视图上
    _bannerView.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height  - height - 100, width, height);
    [self.view addSubview:_bannerView];
//    [_adContainerView setDirection:UISwipeGestureRecognizerDirectionLeft];
    
    // 3 加载并显示广告 注意: 请确保banner视图显示在屏幕内的时候,调用load方法
    [_bannerView loadAndShow];
}

#pragma mark - CXHSDKBannerAdViewDelegate


/**
 广告获取成功
 
 @param bannerView banner实例
 */
- (void)CXH_bannerViewDidReceived:(CXHAdSDKBannerAdView *)bannerView{
    NSLog(@"-------%@--------%@", [UIColor whiteColor], [UIColor clearColor]);
}

/**
 广告拉取失败
 
 @param bannerView banner实例
 @param error 错误描述
 */
- (void)CXH_bannerViewFailToReceived:(CXHAdSDKBannerAdView *)bannerView error:(NSError *)error{
    [_bannerView removeFromSuperview];
    _bannerView = nil;
}

/**
 广告点击
 
 @param bannerView 广告实例
 */
- (void)CXH_bannerViewClicked:(CXHAdSDKBannerAdView *)bannerView loadingPageURL:(nonnull NSString *)loadingPageURL{
    
}

/**
 广告关闭
 
 @param bannerView 广告实例
 */
- (void)CXH_bannerViewClose:(CXHAdSDKBannerAdView *)bannerView{
    dispatch_async(dispatch_get_main_queue(), ^{
        _bannerView = nil;
    });
}

/**
 广告展示
 
 @param bannerView 广告实例
 */
- (void)CXH_bannerViewExposure:(CXHAdSDKBannerAdView *)bannerView{

}

/**
 关闭落地页
 
 @param bannerView 广告实例
 */
- (void)CXH_bannerAdCloseLandingPage:(CXHAdSDKBannerAdView *)bannerView{
    
}



@end
